﻿using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.Activity.Model;
using Shop.Repository;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Collect.lmpl
{
    internal class ActivityCollect : IActivityCollect
    {
        private readonly IActivityRepository _Repository;

        public ActivityCollect ( IActivityRepository repository )
        {
            this._Repository = repository;
        }
        public void CheckStatus ( long id, ActivityStatus status )
        {
            var old = this._Repository.Get(id, c => new
            {
                c.ActivityStatus
            });
            if ( old.ActivityStatus != status )
            {
                throw new ErrorException("shop.activity.status.change");
            }
        }
        public DBShopActivity Get ( long id )
        {
            return this._Repository.Get(id);
        }
        public Result Get<Result> ( long id ) where Result : class, new()
        {
            return this._Repository.Get<Result>(id);
        }
        public long Add ( ActivityAdd add )
        {
            if ( this._Repository.IsExist(c => c.ActivityTitle == add.ActivityTitle) )
            {
                throw new ErrorException("shop.activity.title.repeat");
            }
            return this._Repository.Add(add);
        }
        public Result[] Query<Result> ( ActivityQueryParam query, IBasicPage paging, out int count ) where Result : class
        {
            return this._Repository.Query<Result>(query, paging, out count);
        }
        public void Delete ( DBShopActivity activity )
        {
            if ( activity.ActivityStatus != ActivityStatus.起草 )
            {
                throw new ErrorException("shop.activity.not.allow.delete");
            }
            this._Repository.Delete(activity.Id);
        }
        public bool Set ( DBShopActivity activity, ActivitySet set )
        {
            if ( activity.ActivityStatus != ActivityStatus.起草 )
            {
                throw new ErrorException("shop.activity.not.allow.update");
            }
            else if ( activity.ActivityTitle != set.ActivityTitle && this._Repository.IsExist(c => c.ActivityTitle == set.ActivityTitle) )
            {
                throw new ErrorException("shop.activity.title.repeat");
            }
            return this._Repository.Update(activity, set);
        }

        public bool SubmitAudit ( DBShopActivity activity )
        {
            if ( activity.ActivityStatus == ActivityStatus.待审核 )
            {
                return false;
            }
            else if ( activity.ActivityStatus != ActivityStatus.起草 && activity.ActivityStatus != ActivityStatus.审核未通过 )
            {
                throw new ErrorException("shop.activity.status.change");
            }
            activity.ActivityStatus = ActivityStatus.待审核;
            return this._Repository.Update(activity, "ActivityStatus");
        }
        public bool OffShelf ( DBShopActivity activity )
        {
            if ( activity.ActivityStatus == ActivityStatus.已下架 )
            {
                return false;
            }
            else if ( activity.ActivityStatus != ActivityStatus.已审核 )
            {
                throw new ErrorException("shop.activity.status.change");
            }
            activity.DelistTime = DateTime.Now;
            activity.ActivityStatus = ActivityStatus.已下架;
            return this._Repository.Update(activity, "ActivityStatus", "DelistTime");
        }
        public bool AuditPass ( DBShopActivity activity, long empId )
        {
            if ( activity.ActivityStatus == ActivityStatus.已审核 )
            {
                return false;
            }
            else if ( activity.ActivityStatus != ActivityStatus.待审核 )
            {
                throw new ErrorException("shop.activity.status.change");
            }
            activity.AuditEmpId = empId;
            activity.ActivityStatus = ActivityStatus.已审核;
            activity.OnTime = DateTime.Now;
            return this._Repository.Update(activity, "ActivityStatus", "AuditEmpId", "OnTime");
        }
        public bool AuditNoPass ( DBShopActivity activity, long empId, string opinion )
        {
            if ( activity.ActivityStatus == ActivityStatus.审核未通过 )
            {
                return false;
            }
            else if ( activity.ActivityStatus != ActivityStatus.待审核 )
            {
                throw new ErrorException("shop.activity.status.change");
            }
            activity.AuditEmpId = empId;
            activity.ActivityStatus = ActivityStatus.审核未通过;
            activity.AuditOpinion = opinion;
            return this._Repository.Update(activity, "ActivityStatus", "AuditEmpId", "AuditOpinion");
        }
        public ValidActivityDto[] GetValidActivity ( long skuId, DateTime begin, DateTime end )
        {
            return this._Repository.GetValidActivity(begin, end, skuId);
        }

        public Dictionary<long, string> GetActivityName ( long[] ids )
        {
            if ( ids.IsNull() )
            {
                return [];
            }
            return this._Repository.Gets(ids, a => new
            {
                a.Id,
                a.ActivityTitle
            }).ToDictionary(a => a.Id, a => a.ActivityTitle);
        }
        public string GetActivityName ( long id )
        {
            return this._Repository.Get(id, a => a.ActivityTitle);
        }

        public Result[] Gets<Result> ( long[] ids ) where Result : class
        {
            return this._Repository.Gets<Result>(ids);
        }
    }
}
